package com.yhs.YShop.elastice.service;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.yhs.YShop.elastice.repository.CommodityRepository;
import com.yhs.YShop.model.Commodity;


@Service
public class CommodityServiceImpl implements CommodityService {

	@Autowired
	private CommodityRepository commodityRepository;

	@Override
	public long count() {
		return commodityRepository.count();
	}

	@Override
	public Commodity save(Commodity commodity) {
		return commodityRepository.save(commodity);
	}

	@Override
	public void delete(Commodity commodity) {
		commodityRepository.delete(commodity);
	}

	@Override
	public Iterable<Commodity> getAll() {
		return commodityRepository.findAll();
	}

	public List<Commodity> getByName(String name) {
		List<Commodity> list = new ArrayList<>();
		MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name", name);
		Iterable<Commodity> iterable = commodityRepository.search(matchQueryBuilder);
		iterable.forEach(e -> list.add(e));
		return list;
	}

	public Page<Commodity> pageQuery(Integer pageNo, Integer pageSize, String kw) {
		SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchPhraseQuery("name", kw))
				.withPageable(PageRequest.of(pageNo, pageSize)).build();
		return commodityRepository.search(searchQuery);
	}

	

}
